package com.pk.platform.business.core.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pk.platform.business.core.dao.IFormDao;
import com.pk.platform.business.core.service.IFormService;
import com.pk.platform.domain.form.FormDefine;
import com.pk.platform.domain.form.FormModule;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.Pager;

public class FormServiceImpl implements IFormService {

	private IFormDao formDao;
	
	@Override
	public void delFormDefine(String id) {
		formDao.delete(FormDefine.class, id);
	}

	@Override
	public String delFormModule(String id) {
		String success = StringConverter.resultStr(Constant.SUCCESS, null);
		String failure = StringConverter.resultStr(Constant.FAILURE, "删除失败，已存在表单定义。");
		String result = success;
		FormModule fm = this.queryFormModuleById(id);
		Set<FormDefine> fdSet = fm.getFormDefineSet();
		if(fdSet.size() > 0)
		{
			result = failure;
		}
		else
		{
			formDao.delete(FormModule.class, id);
		}
		return result;
	}

	@Override
	public FormDefine queryFormDefineById(String id) {
		return formDao.get(FormDefine.class, id);
	}

	@Override
	public List<Map<String, Object>> queryFormDefineByModule(String fmId,
			Pager pager) {
		return formDao.queryFormDefineByModule(fmId, pager);
	}

	@Override
	public List<Map<String, Object>> queryFormModule(Pager pager) {
		return formDao.queryFormModule(pager);
	}

	@Override
	public FormModule queryFormModuleById(String id) {
		return formDao.get(FormModule.class, id);
	}

	@Override
	public synchronized String saveFormDefine(FormDefine formDefine) {
		String success = StringConverter.resultStr(Constant.SUCCESS, null);
		String failure = StringConverter.resultStr(Constant.FAILURE, "已存在该编码，请重新输入表单编码。");
		String result = success;
		int count = formDao.defineCountByCode(formDefine.getFormCode(), formDefine.getId());
		FormModule fm = this.queryFormModuleById(formDefine.getFormModule().getId());
		String realCode = fm.getModuleCode() + "_" + formDefine.getFormCode();
		if(StringConverter.isNotNull(formDefine.getId()))
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				FormDefine fd = this.queryFormDefineById(formDefine.getId());
				fd.setFormCode(realCode);
				fd.setFormName(formDefine.getFormName());
				fd.setFormContent(StringConverter.stringToBlob(formDefine.getFormContentStr()));
			}
		}
		else
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				formDefine.setFormCode(realCode);
				formDefine.setFormContent(StringConverter.stringToBlob(formDefine.getFormContentStr()));
				formDao.add(formDefine);
			}
		}
		return result;
	}

	@Override
	public synchronized String saveFormModule(FormModule formModule) {
		String success = StringConverter.resultStr(Constant.SUCCESS, null);
		String failure = StringConverter.resultStr(Constant.FAILURE, "已存在该编码，请重新输入模块编码。");
		String result = success;
		int count = formDao.moduleCountByCode(formModule.getModuleCode(), formModule.getId());
		if(StringConverter.isNotNull(formModule.getId()))
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				FormModule fm = this.queryFormModuleById(formModule.getId());
				if(!fm.getModuleCode().equals(formModule.getModuleCode()))
				{
					Set<FormDefine> fdSet = fm.getFormDefineSet();
					for(FormDefine fd : fdSet)
					{
						String fdCode = fd.getFormCode();
						int index = fdCode.lastIndexOf("_");
						String newCode = formModule.getModuleCode() + "_" + fdCode.substring(index + 1, fdCode.length());
						fd.setFormCode(newCode);
					}
				}
				fm.setModuleCode(formModule.getModuleCode());
				fm.setModuleName(formModule.getModuleName());
			}
		}
		else
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				formModule.setCreateTime(new Date());
				formDao.add(formModule);
			}
		}
		return result;
	}

	public IFormDao getFormDao() {
		return formDao;
	}

	public void setFormDao(IFormDao formDao) {
		this.formDao = formDao;
	}

}
